/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.framework.datasource.TableModel;
import com.jb.web.bean.HtmlItem;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;
import com.jb.web.bean.ValueHolderImpl;


/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
public class ColorPickerRenderer extends DefaultRenderer {
    /**
     * DOCUMENT ME!
     *
     * @param ctx parameter
     * @param itemToRender parameter
     *
     * @return return-value
     */
    public StringBuffer render(WebContext ctx, UIComponent itemToRender) {
        ValueHolderImpl formItem = (ValueHolderImpl) itemToRender;

        return getLovHTML(ctx, formItem);
    }

    protected StringBuffer getLovHTML(WebContext pCtx, ValueHolderImpl pFormItem) {
        StringBuffer html = new StringBuffer("<TABLE border=\"0\"").append(getEditScript(pCtx, pFormItem)).append(" cellpadding=\"0\" cellspacing=\"0\">");
        TableModel tableData = optionData(pCtx, pFormItem, "seeded:color");
        int rows = 9;
        int cols = 9;
        html.append("<TR><TD colspan=\"40\"><DIV id=\"selectedColor\" align=\"center\" >Selected Color</DIV>" +
            "<TD colspan=\"41\"><DIV id=\"sampleColor\" align=\"center\" >Sample</DIV>" + "</TD></TR>");

        for (int i = 0; i < rows; i++) {
            html.append("<TR>");

            for (int k = 0; k < rows; k++)
                for (int j = 0; j < cols; j++) {
                    if (tableData.getMaxValue() <= ((rows * k) + i + (rows * cols * j))) {
                        break;
                    }

                    Iterator row = tableData.getItem((rows * k) + i + (rows * cols * j));

                    if (row == null) {
                        break;
                    }

                    String dbName = (String) row.next().toString();
                    String dbValue = (String) row.next().toString();
                    html.append(renderItem(pFormItem, dbName, dbValue));
                }

            html.append("</TR>\n");
        }

        html = html.append("<TR>");

        for (int j = 0; j < 32; j++) {
            Iterator row = tableData.getItem((9 * 9 * 9) + j);

            if (row == null) {
                break;
            }

            String dbName = (String) row.next().toString();
            String dbValue = (String) row.next().toString();
            html.append(renderItem(pFormItem, dbName, dbValue));
        }

        for (int j = 0; j < 32; j++) {
            Iterator row = tableData.getItem((9 * 9 * 9) + 31 + j);

            if (row == null) {
                break;
            }

            String dbName = (String) row.next().toString();
            String dbValue = (String) row.next().toString();
            html.append(renderItem(pFormItem, dbName, dbValue));
        }

        html.append("</TR>\n");

        html.append("</TABLE>\n");

        return html;
    }

    protected String renderItem(HtmlItem formItem, String dbName, String dbValue) {
        String onMouseOver =  //"this.style.border='1px inset black'"
            "$('sampleColor').style.backgroundColor='" + dbName + "';";

        String onClick = "$('selectedColor').style.backgroundColor='" + dbName + "';$('colorValue').value='" + dbName +
            "';";

        return "<TD width=\"4px\" height=\"8px\" bgcolor=\"" + dbName + "\" onMouseOver=\"" + onMouseOver + "\" " +
        " onClick=\"" + onClick + "\" >" +
        "<img src=\"resources/images/space.gif\" width=\"4px\" height=\"8px\" border=\"0\" />" + "</TD>\n";
    }
}
